# -*- coding: utf-8 -*-
#!/usr/bin/python3

import sqlite3
import os.path as path
from time import localtime, time

class myDbHandler():
    def __init__(self, DbName):
        # 检查由服务器初始化的时候检查？
        # 需要专门写一个配置对象吗？先指定
        self.dbName = DbName
        isFirst = False
        if not path.exists(DbName):
            isFirst = True
        # 然后再初始化一个指针，用于后面的函数使用
        ## 创建数据库链接
        self.__con = sqlite3.connect(self.dbName)
        ## 创建游标
        self.__cur = self.__con.cursor()
        if isFirst:  # 第一次使用数据库，初始化
            self.__initDB()
        pass

    def __initDB(self):  # 初始化数据库
        # 创建柜子表
        table_cabinet_sql = '''
                CREATE TABLE CABINET
                (
                ID INT PRIMIAR YKEY NOT NULL,
                NAME TEXT NOT NULL,
                POSITION TEXT,
                NOTE TEXT
                );
            '''
        table_box_sql = '''
                CREATE TABLE BOXES
                (
                ID INT PRIMARY KEY NOT NULL,
                NAME TEXT NOT NULL,
                BOX_ID INT,
                NOTE TEXT
                );
            '''
        table_file_sql = '''
            CREATE TABLE FILES
            (
            ID INT PRIMARY KEY NOT NULL,
            NO INT NOT NULL,
            FILENAME INT NOT NULL,
            docNo TEXT, /*文号*/
            PERSON TEXT,
            TIME TEXT,
            PAGE_NUM_1 INT NOT NULL, /*页码1*/
            PAGE_NUM_2 INT,
            BOX_ID INT NOT NULL,
            CABINET_ID INT,
            NOTE TEXT
            )
            '''
        ## 依次执行创建语句
        self.__cur.execute(table_cabinet_sql)
        # self.__con.commit()
        self.__cur.execute(table_box_sql)
        # self.__con.commit()
        self.__cur.execute(table_file_sql)
        # self.__con.commit()
    
    def getFiles(self, box, numbers=20):
        pass

    def getBoxList(self):  # 查询所有的盒子
        res = self.__cur.execute('select * from boxes;')
        for row in res:
            pass
    pass